const Sequelize = require('sequelize')

const sequelize = require('../db');

const Role = sequelize.define('role', 
  {
    name: { type: Sequelize.STRING, },
    description: { type: Sequelize.STRING, },
  },
  {
    tableName: 'role',
    // 不要忘记启用时间戳！
    timestamps: true,

    // 不想要 false
    createdAt: 'create_time',

    // 想要 updatedAt 但是希望名称叫做 updateTimestamp
    updatedAt: 'update_time'
  }
);

module.exports.add = async (data) => await Role.create(data)

module.exports.del = async (id) => await Role.destroy({ where: { id } })


module.exports.update = async (data) => {
  let newData = {...data}
  delete newData['id']
  return await Role.update({ ...newData }, {
      where: { id: data.id }
  })
}

module.exports.findOne = async (condition) => {
  let res = await Role.findAll({ where: condition })
  return res[0]
}

module.exports.findAll = async (condition = {}) => {
  return await Role.findAll({ where: condition })
}